Skip to content

Fix trig signs under GCC fast-math; drop the signed-zeros pragma - #1466

Open
assaferan wants to merge 2 commits into
xtensor-stack:masterfrom
assaferan:fix-gcc-fast-math-signmask
Open

assaferan wants to merge 2 commits into
xtensor-stack:masterfrom
assaferan:fix-gcc-fast-math-signmask

Conversation

@assaferan

Copy link
Copy Markdown

Fixes #1461.

To answer the questions on the issue: the pragma region doesn't protect signmask, and a reassociation barrier does.

What the region does today

  • It doesn't protect signmask. The constants are XSIMD_INLINE, so they're inlined into the fast-math caller, and -fno-signed-zeros applies there again. In the trig kernels the sign bit comes from select(c, signmask, 0.). Since -0.0 and 0.0 compare equal under -fno-signed-zeros, GCC folds that select to a single constant, and sin, cos and sincos get the wrong sign in some quadrants. This happens with the region in place, and on GCC 13 to 15 the region works as written there: push and pop balance.
  • On GCC 16 it leaks (GCC 16 with -ffast-math: xsimd_constants.hpp leaves its #pragma GCC push_options region open #1461). #pragma GCC optimize("signed-zeros") undefines __FAST_MATH__ inside the region, so the closing #if is false and pop_options never runs. GCC 13.4, 14.4 and 15.3 keep __FAST_MATH__ defined there.

The change

  • Remove the region from xsimd_constants.hpp.
  • Pass the mask through detail::reassociation_barrier at the four select(..., signmask, 0.) sites in xsimd_common_trigo.hpp: cos, sin, and the two in sincos.

The barrier is a no-op unless XSIMD_REASSOCIATIVE_MATH. With the region gone, the #ifndef __FAST_MATH__ block around infinity and minusinfinity is seen under the translation unit's own flags again on GCC 16, as on older GCC.

Reproducer

// fsin.cpp, compiled with: g++ -std=c++17 -O2 -ffast-math -march=x86-64-v3 -c fsin.cpp
#include <xsimd/xsimd.hpp>
#include <cstddef>

void fsin(const float* x, float* y, std::size_t n)
{
    using B = xsimd::batch<float>;
    for (std::size_t i = 0; i + B::size <= n; i += B::size)
        xsimd::sin(B::load_unaligned(x + i)).store_unaligned(y + i);
}

Called from another translation unit, built without -ffast-math, on x = 0.5, 1.5, ..., 7.5. On master with GCC 16.2, five of the eight results have the wrong sign. For example, sin(0.5) gives -0.4794 and sin(5.5) gives +0.7055. With this PR all eight are right.

Testing

All runs on x86-64, conda-forge GCC 13.4, 14.4, 15.3 and 16.2.

Signs. sin, cos, tan, sincos, bitofsign and sign are computed on 800 points over all four quadrants, in float and double. The computing translation unit uses -ffast-math; a separate checking translation unit, built without it, compares raw sign bits against std:::

build master master, push/pop balanced region removed this PR
-O2 -march=x86-64-v3 sin 460, cos 381 wrong same same 0
-O2 -march=x86-64-v2 sin 460, cos 381 wrong same same 0
-O2 (SSE2) 0 0 0 0
-O3 -march=native (AVX-512) 0 0 0 0

The x86-64-v3 and AVX-512 rows were run on all four GCC versions, and the x86-64-v2 and SSE2 rows on GCC 13.4 and 16.2. The counts are the same on every version run, and for float and double. tan, bitofsign and sign are right in every build. SSE2 selects with bitwise operations and AVX-512 with mask registers, so neither has a float select to fold.

test_xsimd (-DBUILD_TESTS=ON; failed test cases):

build master push/pop balanced this PR
GCC 16, no fast-math, native 0 of 444 0 of 444
GCC 16, -ffast-math, -O3 native 9 (32 assertions), then SIGSEGV 3 (4 assertions), then SIGSEGV same as balanced
GCC 13, -ffast-math, -O3 native 1 same
GCC 16, -ffast-math -mfma, TARGET_ARCH=avx2, -O2 SIGSEGV after 48 test cases, in the complex tests 3, then SIGSEGV
  • On GCC 13, "push/pop balanced" is what master already does.
  • In the AVX2 build without this PR, the complex exponential and hyperbolic tests also fail en masse. With the crashing cases excluded, 54,998 of 61,923 assertions failed; the first failures show the wrong sign on the imaginary part, which comes from sin. With this PR the AVX2 build matches the AVX-512 one: with the order-dependent crash excluded, only the atan2(±0, ±0) checks fail.

Fast-math failures left as they are. These fail before and after this PR, and on master:

  • the atan2(±0, ±0) checks in test_trigonometric.cpp, which assume signed zeros;
  • an exact CHECK_EQ on expm1 (GCC 16);
  • a SIGSEGV in "xsimd api | float types functions" on GCC 16. It appears only in a full run of the suite: the test case passes when run alone.

The CI's fast-math job uses Clang at -O0 -fno-inline, where none of this shows. A GCC job at -O2 with SSE4.1 or AVX2 would catch both issues, but it would also need the three items above addressed or skipped first.

The GCC-only pragma region around the constants (push_options and
optimize("signed-zeros")) was meant to keep signmask (-0.0) from being
folded to 0 under -ffast-math. It does not: the constants are
XSIMD_INLINE, so they are inlined into the fast-math caller. There -0.0
and 0.0 compare equal, and select(c, signmask, 0.) folds to a single
constant, so sin, cos and sincos get the wrong sign in some quadrants
(GCC 13 to 16, SSE4.1 and AVX2).

On GCC 16 the region also leaks: optimize("signed-zeros") undefines
__FAST_MATH__ inside it, so the closing #if is false and pop_options
never runs (xtensor-stack#1461).

Remove the region and pass the mask through
detail::reassociation_barrier at the four select sites in the trig
kernels.
template <class B>
XSIMD_INLINE B opaque_signmask() noexcept
{
B mask = constants::signmask<B>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make it so it does not impact non -ffast-math code :-)

Without XSIMD_REASSOCIATIVE_MATH, the trig kernels now reach
constants::signmask itself through a namespace alias, so their object
code is byte-identical to master's at -O0 to -O3 and -Os (GCC 13.4 and
16.2; SSE4.1, AVX2 and AVX-512). Under -ffast-math the code is the same
as in the previous commit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GCC 16 with -ffast-math: xsimd_constants.hpp leaves its #pragma GCC push_options region open

2 participants